package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.UserAssistantZsSetData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserAssistantZsSetDaoImpl extends TempDataObjectDao<UserAssistantZsSetData> {

    public static String getUserAssistantZsSetDataSql = "select * from `t_u_assistant_zs_set` where `userId`=?";
    public UserAssistantZsSetData getUserAssistantZsSetData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserAssistantZsSetDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserAssistantZsSetDataSql = "update `t_u_assistant_zs_set` set isOpen=?,typeList=? where userId=?";
    @Override
    protected boolean updateData(UserAssistantZsSetData userAssistantZsSetData) {
        userAssistantZsSetData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BOOLEAN, userAssistantZsSetData.isOpen()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAssistantZsSetData.getTypeList())));
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantZsSetData.getUserId()));
        if (getDb().executeNoneQuery(updateUserAssistantZsSetDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserAssistantZsSetDataSql = "insert into `t_u_assistant_zs_set`(userId,isOpen,typeList) values (?,?,?)";
    @Override
    protected boolean addData(UserAssistantZsSetData userAssistantZsSetData) {
        userAssistantZsSetData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantZsSetData.getUserId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userAssistantZsSetData.isOpen()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userAssistantZsSetData.getTypeList())));
        if (getDb().executeNoneQuery(insertUserAssistantZsSetDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserAssistantZsSetData parseData(ResultSet rs) throws SQLException {
        UserAssistantZsSetData userAssistantZsSetData = new UserAssistantZsSetData();
        userAssistantZsSetData.setUserId(rs.getLong("userId"));
        userAssistantZsSetData.setOpen(rs.getBoolean("isOpen"));
        userAssistantZsSetData.setTypeList(JSON.parseObject(rs.getString("typeList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));

        return userAssistantZsSetData;
    }
}
